package com.happy016.controller;

import com.happy016.entity.User;
import org.springframework.web.bind.annotation.*;


/*
 *  GET /collection：返回资源对象的列表（数组）
 *  GET /collection/resource：返回单个资源对象
 *  POST /collection：返回新生成的资源对象
 *  PUT /collection/resource：返回完整的资源对象
 *  PATCH /collection/resource：返回完整的资源对象
 *  DELETE /collection/resource：返回一个空文档
 */
/**
 * Restful风格请求示例
 * @author Linyb
 * create on 2017/8/12 22:37
 */
@RestController
public class RestfulController {

    private User user ;

    /**
     * GET获取操作
     * @author Linyb
     * create on 2017/8/12 23:07
     */
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public User get() {
        return user == null ? new User(1,"DEMO",2) : user;
    }

    /**
     * GET获取具体的一个
     * @author Linyb
     * create on 2017/8/12 23:08
     */
    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    public User get(@PathVariable Integer id) {
        return new User(id,"DEMO"+id,id);
    }

    /**
     * POST执行修改 或者 新增
     * @author Linyb
     * create on 2017/8/12 23:07
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public User add(User user){
        this.user = user;
        return user;
    }

    /**
     * PUT执行 修改操作
     * @author Linyb
     * create on 2017/8/12 23:07
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public User update(User user){
        this.user = user;
        return user;
    }

    /**
     * DELETE执行删除操作
     * @author Linyb
     * create on 2017/8/12 23:07
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public void delete(@PathVariable Integer id){
        if (user != null && id.equals(user.getId())){
            this.user = null;
        }
    }

}
